package com.anjufang.controller;

import com.anjufang.entity.SourceHouse;
import com.anjufang.entity.SourceUser;
import com.anjufang.service.user.HouseExcelService;
import com.anjufang.utils.Result;
import com.anjufang.utils.excel.ExcelImport;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("house/excel")
public class HouseExcelController {

    @Autowired
    private HouseExcelService sourceHouseService;

    /**
     * 导入房源
     * @param request
     * @return
     */
    @PostMapping("import")
    public Result importExcel(HttpServletRequest request) {

        ExcelImport excelImport = new ExcelImport();
        try {
            List<Object[]> dataList = excelImport.importExcel(request);
            if (dataList == null || dataList.size() == 0) {
                return Result.error("excel内容为空,导入失败!");
            }
            sourceHouseService.importExcel(dataList);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();

            return Result.error("导入失败，请检查数据是否正确");
        }
    }
}
